﻿using EPiServer.DataAbstraction;
using PageTypeBuilder;
using TemplateFoundation.Exceptions;

namespace TemplateFoundation.PageTypes
{
    /// <summary>
    /// Extension methods for PageType objects
    /// </summary>
    public static class PageTypeExtensions
    {
        /// <summary>
        /// Returns the name of a page type
        /// </summary>
        /// <param name="pageType"></param>
        /// <returns></returns>
        public static string GetPageTypeName(this TypedPageData pageType)
        {
            int? pageTypeId = PageTypeResolver.Instance.GetPageTypeID(pageType.GetType());

            if (!pageTypeId.HasValue)
            {
                throw new TemplateFoundationException("The specified type does not correspond to a valid page type");
            }

            return PageType.Load(pageTypeId.Value).Name;
        }
    }
}